package com.ml.mall.service.asyn;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ml.mall.bean.req.company.CompanyDto;
import com.ml.mall.bean.req.product.SysProductNewReq;
import com.ml.mall.bean.res.company.CompanyQueryInfoRes;
import com.ml.mall.consts.MailConstants;
import com.ml.mall.consts.MailContentConsts;
import com.ml.mall.entity.category.SysACategory;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.model.MailMessage;
import com.ml.mall.service.category.SysACategoryService;
import com.ml.mall.service.company.SysCompanyService;
import com.ml.mall.service.mail.MailService;
import com.ml.mall.service.notifation.SysANotifationService;
import com.ml.mall.service.sys.SysUserService;
import com.ml.mall.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * Created by kyyang on 2021/7/27 23:25
 */
@Component
public class ProductMailServiceImplAsyn {

    @Autowired
    private MailService mailService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysCompanyService sysCompanyService;

    @Autowired
    private SysACategoryService sysACategoryService;

    @Autowired
    private SysANotifationService sysANotifationService;




    /**
     * @description: 创建产品发送邮件 通知管理员
     * @author kyyang
     * @date 2021/7/29 23:04
     * @version 1.0
     */
    @Async
    public void sysProductNewMailAsyn(SysProductNewReq sysProductNewReq) {

        MailMessage mailMessage = mailService.sendForNewProductUploaded("");
        mailMessage.setContent(mailMessage.getContent().replaceAll("mainPicUrl", MailContentConsts.MAIN_PIC_URL));
        mailMessage.setContent(mailMessage.getContent().replaceAll("general_email",MailContentConsts.GENERAL_EMAIL));
        mailMessage.setContent(mailMessage.getContent().replaceAll("system_url",MailContentConsts.SYSTEM_URL));
        mailMessage.setContent(mailMessage.getContent().replaceAll("contact_no",MailContentConsts.CONCAT));

        CompanyDto dto = new CompanyDto();
        dto.setCompanyId(sysProductNewReq.getSupplierId());
        CompanyQueryInfoRes companyQueryInfoRes = sysCompanyService.queryOneCompany(dto);
        if(companyQueryInfoRes==null){
            return;
        }
        QueryWrapper<SysACategory> query = new QueryWrapper<>();
        query.eq("category_id",sysProductNewReq.getCategoryId());
        SysACategory one = sysACategoryService.getOne(query);
        String category=null;
        if(one != null){
            category=one.getCategoryName();
            if(one.getRootCategoryId() != null){
                QueryWrapper<SysACategory> query2 = new QueryWrapper<>();
                query2.eq("category_id",one.getRootCategoryId());
                category=sysACategoryService.getOne(query2).getCategoryName();
            }
        }
        mailMessage.setContent(mailMessage.getContent().replace("company_name",companyQueryInfoRes.getCompanyName()));
        mailMessage.setContent(mailMessage.getContent().replace("product_name",sysProductNewReq.getProductName()));
        mailMessage.setContent(mailMessage.getContent().replace("CATE_GORY",category==null?"":category));
        mailMessage.setContent(mailMessage.getContent().replace("product_description",sysProductNewReq.getDescription()));
        mailMessage.setContent(mailMessage.getContent().replace("product_brand",sysProductNewReq.getBrand()));
        mailMessage.setContent(mailMessage.getContent().replace("SUB_CATEGORY",one==null?"":one.getCategoryName()));


        if(mailMessage!=null){
            //通知管理员
            sysANotifationService.notifyAdmin(mailMessage);
            //发送邮件
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.in("role_id", Arrays.asList(new Integer[]{1,2,4,5}));
            List<SysUser> list = sysUserService.list(wrapper);
            String content=mailMessage.getContent();
            if(list!=null&&list.size()>0){
                for(SysUser u:list){
                    mailMessage.setReceiverEmail(u.getEmail());
                    mailMessage.setContent(mailMessage.getContent().replace("admin_name",u.getName()));
                    MailUtils.doSendEmail1(mailMessage);
                    mailMessage.setContent(content);
                }
            }

        }

    }
}
